﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;

namespace MTSGui
{
    public class UpdateCheck
    {

        public static UpdateInfo CheckForUpdate()
        {
            AssemblyName AppAssembly = Assembly.GetExecutingAssembly().GetName();
            string CheckUrl = String.Format(
                "http://mts.davejk.net/update/{0}.xml",
                AppAssembly.Version);

            Console.WriteLine("Checking for updates from {0}", CheckUrl);

            try
            {
                XmlDocument UpdateInfo = new XmlDocument();
                UpdateInfo.Load(CheckUrl);

                XmlNode UpdateNode = UpdateInfo.SelectSingleNode("/MTSUpdate/Update");
                if (UpdateNode == null)
                    return new UpdateInfo { UpdateAvailable = false };

                UpdateInfo Info = new UpdateInfo { UpdateAvailable = true };
                Info.Version = UpdateNode.Attributes["Version"].Value;
                Info.Text = UpdateNode.SelectSingleNode("Text").InnerText;
                Info.Link = UpdateNode.SelectSingleNode("Link").InnerText;

                return Info;
            }
            catch (Exception ex)
            {
                return new UpdateInfo { UpdateAvailable = false };
            }
        }

    }

    public struct UpdateInfo
    {
        public bool UpdateAvailable;

        public string Version;
        public string Text;
        public string Link;
    }

}
